Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@shikijs/vscode-textmate

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@shikijs/vscode-textmate

Shiki's fork of `vscode-textmate`

  • 9.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
458K
decreased by-33.62%
Maintainers
0
Weekly downloads
 
Created

What is @shikijs/vscode-textmate?

@shikijs/vscode-textmate is a library that provides TextMate grammar support for syntax highlighting in JavaScript and TypeScript applications. It allows developers to use TextMate grammars to tokenize source code and apply syntax highlighting, similar to how Visual Studio Code does it.

What are @shikijs/vscode-textmate's main functionalities?

Loading a TextMate Grammar

This code demonstrates how to load a TextMate grammar for JavaScript using the @shikijs/vscode-textmate package. It reads a grammar file and parses it into a usable format.

const vscodeTextmate = require('@shikijs/vscode-textmate');
const fs = require('fs');
const path = require('path');

async function loadGrammar() {
  const registry = new vscodeTextmate.Registry({
    loadGrammar: (scopeName) => {
      if (scopeName === 'source.js') {
        return new Promise((resolve, reject) => {
          fs.readFile(path.join(__dirname, 'JavaScript.tmLanguage.json'), 'utf8', (err, data) => {
            if (err) {
              reject(err);
            } else {
              resolve(vscodeTextmate.parseRawGrammar(data, path.join(__dirname, 'JavaScript.tmLanguage.json')));
            }
          });
        });
      }
      return null;
    }
  });

  const grammar = await registry.loadGrammar('source.js');
  console.log(grammar);
}

loadGrammar();

Tokenizing a Line of Code

This code demonstrates how to tokenize a line of JavaScript code using a loaded TextMate grammar. It shows how to initialize the rule stack and tokenize a given line of code.

const vscodeTextmate = require('@shikijs/vscode-textmate');

async function tokenizeLine(grammar, line) {
  const ruleStack = vscodeTextmate.INITIAL;
  const lineTokens = grammar.tokenizeLine(line, ruleStack);
  console.log(lineTokens);
}

async function main() {
  const registry = new vscodeTextmate.Registry({
    loadGrammar: async (scopeName) => {
      if (scopeName === 'source.js') {
        const grammar = await fetch('path/to/JavaScript.tmLanguage.json').then(res => res.json());
        return vscodeTextmate.parseRawGrammar(JSON.stringify(grammar));
      }
      return null;
    }
  });

  const grammar = await registry.loadGrammar('source.js');
  tokenizeLine(grammar, 'const x = 42;');
}

main();

Other packages similar to @shikijs/vscode-textmate

FAQs

Package last updated on 06 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc